package com.outsider.thunder.controller;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.PropKit;
import com.jfinal.weixin.sdk.api.CustomServiceApi;
import com.jfinal.weixin.sdk.api.SnsAccessToken;
import com.jfinal.weixin.sdk.api.SnsAccessTokenApi;
import com.outsider.interceptor.ResponseHeaderInterceptor;
import com.outsider.thunder.service.ThunderService;
import com.outsider.util.Const;
import com.outsider.util.thunder.Thunder;
import com.outsider.weixin.dao.User;


@Before(ResponseHeaderInterceptor.class)
public class ThunderController extends Controller{
	private ThunderService thunderService=new ThunderService();
	
	public void delete(){
		Long taskID=getParaToLong();
		String openID=getPara(Const.OPEN_ID);
		Integer userID=User.dao.findIDbyOpenID(openID);
		if (thunderService.delete(taskID, userID))
		  renderJson(Const.RESULT, Const.SUCCESS);
		else
		  renderJson(Const.RESULT,Const.FAIL);
	}
	
	public void getAll(){
/*		int page=getParaToInt("page");
		int row=getParaToInt("row");*/
		String openID=getPara(Const.OPEN_ID);
		Integer userID=User.dao.findIDbyOpenID(openID);
/*		String[] sortField=null;
		String[] sortType=null;
		if (getPara("sortField")!=null){
			sortField=getPara("sortField").split(",");
			sortType=getPara("sortType").split(",");
		}*/
        setAttr("page",thunderService.getAll(userID));
        renderJson();
	} 
	public void addTask(){
		String url=getPara("url");
		String openID=getPara("openID");
	    renderText("添加任务中,请稍后");
	    
	    Thread thread=new Thread(new Runnable() {
			@Override
			public void run() {
				 Integer bo=thunderService.addThunderTask(url,openID);	 
				 String msg;
				 if (bo==Const.THUNDER_TASK_ADD_EXIST) return;
				 if (bo==Const.THUNDER_TASK_ADD_SUCCESS) msg="新增链接任务成功";
				 else msg="新增链接任务失败";
				 CustomServiceApi.sendText(openID, msg);
			}
		});
		 thread.start();
		 return;
	}
	

}
